const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const { notFoundMiddleware, errorMiddleware, successMiddleware } = require('./middlewares/index');
const userRoutes = require('./routes/user');
const path = require('path');
const fileRoutes = require('./routes/files');
const folderRoutes = require('./routes/folders');
const shareRoutes = require('./routes/shares');
const cors = require('cors');
const app = express();
// 设置 CORS 选项
const corsOptions = {
  origin: '*', // 替换为您的前端应用的域名
  optionsSuccessStatus: 200 // 一些旧版浏览器 (IE11, 各种SmartTV) 会默认使用 204
};

// 应用 CORS 中间件
app.use(cors(corsOptions));
// 解析请求体中的 JSON 数据
app.use(express.json());

// 设置静态文件目录
app.use(express.static('uploads'));

// 假设你的上传目录是 'uploads'
const uploadsDir = path.join(__dirname, 'uploads');
// 提供静态文件服务
app.use('/uploads', express.static(uploadsDir));

// 路由配置
app.use(successMiddleware);
// app.use('/api/files', fileRoutes);
app.use('/api', userRoutes);
app.use('/api/files', fileRoutes);
app.use('/api/folders', folderRoutes);
app.use('/api/shares', shareRoutes);
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// 应用中间件
app.use(notFoundMiddleware);
app.use(errorMiddleware);

// 启动服务器
const port = 9001;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});